package com.itheima.controller;


import com.itheima.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/user")
@ResponseBody  // 响应体
public class UserController {

    @RequestMapping("/save")  // 设置当前控制器方法请求访问路径    别名
    public String save1() {
        System.out.println("我是 UserController 的 save1() ...");
        return "'info':'springmvc'";
    }

    @RequestMapping("/p1")
    public String p1(String name, int age) {
        System.out.println("name " + name);
        System.out.println("age " + age);
        return "ok!!";
    }

    @RequestMapping("/p2")
    public String p2(User user) {
        System.out.println(user);
        return "ok!!";
    }

    @RequestMapping("/p3")  // 测试 pojo 类嵌套使用
    public String p3(User user) {
        System.out.println(user);
        return "ok!!";
    }

    @RequestMapping("/p4")
    public String p4(String[] strings) {
        System.out.println(Arrays.toString(strings));
        return "ok!!";
    }

    @RequestMapping("/p5")
    public String p5(@RequestParam List<String> list) {
        System.out.println(list);
        return "ok!!";
    }

    @RequestMapping("/p6")
    public String p6(@RequestParam Map map) {
        System.out.println(map);
        return "ok!!";
    }

    @RequestMapping("/p7")
    public String p7(Date date,
                     @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:ss:mm") Date date2)  {
        System.out.println(date);
        System.out.println(date1);
        System.out.println(date2);
        return "ok!!";
    }


    @RequestMapping("/p8")
    public String p8(@RequestBody List<String> likes){
        System.out.println("list common(json)参数传递 list ==> "+likes);
        return "{'module':'list common for json param'}";
    }

    @RequestMapping("/p9")
    @ResponseBody
    public String p9(@RequestBody User user){
        System.out.println("pojo(json)参数传递 user ==> "+user);
        return "{'module':'pojo for json param'}";
    }

    @RequestMapping("/p10")
    @ResponseBody
    public String p10(@RequestBody List<User> list){
        System.out.println("list pojo(json)参数传递 list ==> "+list);
        return "{'module':'list pojo for json param'}";
    }

    @RequestMapping("/p11")
    @ResponseBody
    public String p11(){
        System.out.println("t跳转");
        return "index.html";
    }

    @RequestMapping("/p12")
    @ResponseBody
    public List<User> p12(){
        System.out.println("返回json集合数据");
        User user1 = new User();
        user1.setName("传智播客");
        user1.setAge(15);

        User user2 = new User();
        user2.setName("黑马程序员");
        user2.setAge(12);

        List<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);

        return userList;
    }

    @PostMapping
    public String save(){
        System.out.println("save");
        return "{'info':'heiheisave'}";
    }


    @GetMapping
    public String find(){
        System.out.println("find");
        return "{'info':'heiheifind'}";
    }

    @PutMapping
    public String put(){
        System.out.println("修改");
        return "{'info':'edit'}";
    }

    @DeleteMapping
    public String delete(){
        System.out.println("删除");
        return "{'info':'delete'}";
    }

    // 需要传递参数
    @GetMapping("/{id}/{name}")
    public String find(@PathVariable String id,@PathVariable String name){
        System.out.println("find"+ id);
        return "{'info':'heiheifindcanshu'}";
    }

























}
